package pageShare

import (
	"context"
	"encoding/json"

	"laundry-service/app/client/internal/svc"
	"laundry-service/app/client/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetPageShareListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetPageShareListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPageShareListLogic {
	return &GetPageShareListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetPageShareListLogic) GetPageShareList(req *types.Request) (resp []types.CreatePageShareReply, err error) {
	userId, _ := l.ctx.Value("userId").(json.Number).Int64()
	_, err = l.svcCtx.Db.GetAdminById(int(userId))
	if err != nil {
		return nil, err
	}

	pageShareList, err := l.svcCtx.Db.GetPageShareList()
	if err != nil {
		l.Logger.Errorf("get page share list error: %v", err)
		return nil, err
	}

	resp = make([]types.CreatePageShareReply, len(pageShareList))
	for i, pageShare := range pageShareList {
		resp[i] = types.CreatePageShareReply{
			Id:          pageShare.ID,
			CreatedAt:   pageShare.CreatedAt.Format("2006-01-02 15:04:05"),
			UpdatedAt:   pageShare.UpdatedAt.Format("2006-01-02 15:04:05"),
			Path:        pageShare.Path,
			Title:       pageShare.Title,
			Description: pageShare.Description,
			ImageUrl:    pageShare.ImageUrl,
		}
	}

	return resp, nil
}
